/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server_relay;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Athéna
 */
public class client_exemple {

    private BufferedWriter writerc = null;// Buffer d'écriture pour émettre
    private BufferedReader readerc = null;//Buffer de lecture pour la réception
    private int port;

    //Constructeur
    public client_exemple(int port, BufferedWriter w, BufferedReader r) {
        this.writerc = w;
        this.readerc = r;
        this.port = port;
        this.init();
    }

    //@Override
   /* public void run(){
    System.out.println("Lancement thread serveur fait");
    while(true){
    System.out.println("Réception question "+this.receiveQuestion());
    }

    }*/
    //Inialisation: Création du socket
    public void init() {
        System.out.println("init client");
        try {
            Socket sockclient = new Socket("localhost", 4000);
            System.out.println("socket client crée");
            InputStream in = sockclient.getInputStream();
            OutputStream out = sockclient.getOutputStream();
            writerc = new BufferedWriter(new OutputStreamWriter(out));
            readerc = new BufferedReader(new InputStreamReader(in));
        } catch (UnknownHostException ex) {
            Logger.getLogger(client_exemple.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(client_exemple.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    //Méthode permettant d'envoyer le paquet contenant le nombre de réponses possibles pour une question donnée
    public void sendResponse(String r) {
        //System.out.println("Envoi de réponse Client");

        try {
            writerc.write(r, 0, r.length());//Ecriture pour envoyer le flux
            writerc.newLine();
            writerc.flush();
        } catch (IOException ex) {
            System.out.println("Exception in SendResponse Method: " + ex);
        }
    }

    //Méthode permettant de recevoir les réponses provenant du relais
    public String receiveQuestion() {
        String q = null;
        try {
            q = readerc.readLine();
        } catch (IOException ex) {
            System.out.println("Exception in receiveQuestion Method: " + ex);
        }
        return q;
    }

    public static void main(String[] args) {

        System.out.println("Début");
        BufferedWriter wc2s = null;
        BufferedReader rs2c = null;

        client_exemple client = new client_exemple(4000, wc2s, rs2c);
        System.out.println("Création client faite");

        int ex = 0;
        int r = 0;
        String m = null;
        while (ex <= 10) {
            m = String.valueOf(ex);
            System.out.println("Réception question " + client.receiveQuestion());
            System.out.println("Envoi de réponses Client");
            client.sendResponse(m);
            ex = ex + 1;
        }
    }
}




